
# =====================================
# solve error：UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
# =====================================
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #改变标准输出的默认编码


from urllib import request
from urllib import parse
from http.cookiejar import CookieJar

homepageurl  = 'https://i.meishi.cc/cook.php?id=5252917'
loginpageurl = 'https://i.meishi.cc/login_t.php?username=xxx&login_type=2&password=xxx&cookietime=on'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
}

query_data = parse.urlencode(
    {
        'username': 'xxx',
        'password': 'xxx'
    }
)



#1.登录 
#1.1 创建cookiejar对象
cookiejar = CookieJar()
#1.2 使用cookiejar创建一个HTTPCookieProcess对象
handler = request.HTTPCookieProcessor(cookiejar)
#1.3 使用上一步创建的handler创建一个opener
opener = request.build_opener(handler)
#1.4 使用opener发送登录请求
req = request.Request(loginpageurl, data=query_data.encode('utf-8'))
opener.open(req)

#2 访问homepage 
rq = request.Request(homepageurl, headers=headers)
resp = opener.open(rq)
print(resp.read().decode())